{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b8b76da9",
   "metadata": {},
   "source": [
    "# 语音录制\n",
    "\n",
    "* 1. 下载和安装模块：pip install 模块名称\n",
    "> 1.SpeechRecognition  \n",
    "> 2.Pyaudio  \n",
    "> 3.pydub\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "3d9630d1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import speech_recognition\n",
    "r = speech_recognition.Recognizer()\n",
    "with speech_recognition.Microphone() as source:\n",
    "    audio = r.listen(source)\n",
    "# 将数据保存到wav文件中\n",
    "with open(\"1.wav\", \"wb\") as f: \n",
    "    f.write(audio.get_wav_data(convert_rate=16000))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6c98fcf",
   "metadata": {},
   "source": [
    "# 2.语音识别（百度的ASR） auto speech recognition  \n",
    "* I/O:音频-->文本文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "a445d3ee",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bdasr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "id": "cd32aee2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Request time cost 0.731309\n",
      "{\"corpus_no\":\"7248595918590989260\",\"err_msg\":\"success.\",\"err_no\":0,\"result\":[\"你好。\"],\"sn\":\"919973772331687695253\"}\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'你好。'"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bd_asr_result = bdasr.asr('1.wav')\n",
    "bd_asr_result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1dfd4dda",
   "metadata": {},
   "source": [
    "# 3.自然语言处理（NLP）  \n",
    "* I/O：文本-->文本（文本的生成：文本的理解，文本的处理，文本的生成）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "b0c3ae59",
   "metadata": {},
   "outputs": [],
   "source": [
    "qa = {\n",
    "    '你好。':\"你也好，我是智能小新，很高兴为你服务\"\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "94a6df4e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import opchat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "0d1acb59",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'你也好，我是智能小新，很高兴为你服务'"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result = qa.get(bd_asr_result)\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ab6c20f",
   "metadata": {},
   "source": [
    "# 4.语音合成（百度的TTS）text to speech  \n",
    "* I/O：文本文件-->音频文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "346415cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import bdtts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "3e93c363",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pydub import AudioSegment\n",
    "from pydub.playback import play\n",
    "song = AudioSegment.from_wav('result.wav')\n",
    "play(song)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "32f6ee3f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb9617d9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
